<?php

namespace addons\csmwj\library\xapp\csmwj\base;

use addons\csmwj\library\xcore\xcore\base\XcABackend;

abstract class XpADashboard extends XcABackend
{
    /**
     * 获取前后几天的时间范围,通常用于取时间轴计算日期范围
     * $getDateArray = $this->getDateArray(-3,+3);
     */
    protected function getDateArray($beginDateSep, $endDateSep)
    {
        $beginDate = date('Y-m-d', strtotime("{$beginDateSep} day"));
        $endDate = date('Y-m-d', strtotime("{$endDateSep} day"));
        return [$beginDate, $endDate];
    }

    /**
     * 将list转换为echart的options data,通常用于x轴为日期,柱状图和折线图的options的值生成
     * 说明：
     *      当参数：(-2, +2, $list, 'name', 'applydate', ['totalhours', 'totaluser'])
     *      则返回：
     *      sr = [
     *          'applydate_array' => ['2020-06-14', '2020-06-15', '2020-06-16', '2020-06-17', '2020-06-18', '2020-06-19', '2020-06-20'],
     *          'series_object_array' => ["张江分部一号楼", "茶几大楼"],
     *          'series_data_array_array' => {
     *              'totalhours'=>[
     *                 [23, 214, 18, 215, 27, 28, 25],
     *                  [26, 24, 18, 222, 213, 20, 27]
     *              ],
     *              'endDateSep'=>[
     *                  [23, 214, 18, 215, 27, 28, 25],
     *                  [26, 24, 18, 222, 213, 20, 27]
     *              ],       
     *          } 
     *      ])  
     */
    protected function getObjectDateOption($beginDateSep, $endDateSep, $list, $objectFieldName, $applyFieldName, $valueFieldNames)
    {
        $applydate_array = [];
        for ($i = $beginDateSep; $i <= $endDateSep; $i++) {
            $applydate_array[] = date('Y-m-d', strtotime("{$i} day"));
        }

        $series_key_array = [];
        foreach ($list as $item) {
            if (!in_array($item->$objectFieldName, $series_key_array)) {
                $series_key_array[] = $item->$objectFieldName;
            }
        }

        //init series_data_array
        $series_data_array_array = [];
        foreach ($valueFieldNames as $fieldName) {
            $series_data_array = [];
            foreach ($series_key_array as $k => $v) {
                $item = [];
                foreach ($applydate_array as $kk => $vv) {
                    $item[$kk] = 0;
                    //$item[$kk] = Random::numeric(1);
                }
                $series_data_array[] = $item;
            }
            $series_data_array_array[$fieldName] = $series_data_array;
        }


        //$series_data_array = [];
        foreach ($series_key_array as $rowIndex => $key) {
            foreach ($applydate_array as $fieldIndex => $applyDate) {
                foreach ($list as $item) {
                    if ($key == $item->$objectFieldName && $applyDate == $item->$applyFieldName) {
                        foreach ($valueFieldNames as $fieldName) {
                            $series_data_array_array[$fieldName][$rowIndex][$fieldIndex] = (int)$item->$fieldName;
                        }
                    }
                }
            }
        }
        //CDebug::showList($series_data_array_array);
        $sr = [
            'applydate_array' => $applydate_array,
            'series_object_array' => $series_key_array,
            'series_data_array_array' => $series_data_array_array
        ];


        return $sr;
    }
}
